import { set } from 'date-fns'
import { truncate } from 'node:fs/promises'
import { defineStore } from 'pinia'

export const useImgStore = defineStore('imgStore', { // ✅ 建议使用有意义的 store 名称
  state: () => ({
    imgVisable: false as boolean,
    imgRow: null as File | null, // ✅ 明确的类型声明
    imgUrl: '' as string,
    uploadPromise: null as Promise<void> | null
  }),
  actions: {
    setImgRow(img: File) {
      this.imgRow = img
    },
    setImgRowNull() {
      this.imgRow = null
    },
    setImgVisable(boolean: boolean) { // ✅ 修正方法名拼写
      this.imgVisable = boolean
    },

    setUploadPromise(promise: Promise<void>) {
      this.uploadPromise = promise
    },
    // 使用 $patch 确保响应式更新
    setImgUrl(url: string) {
      this.$patch({ imgUrl: url })
    },
    setImgUrlNull() {
      this.imgUrl = ''
    },
  },
  getters: {
    getImgVisable: (state) => state.imgVisable,
    // 可选的 getters
  }
}) // ✅ 闭合大括号和括号